﻿
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using Techstar.Dialog;
using SmartMD.DataModel.Entity;


namespace SmartMD.Manager.SysInfo
{
    public partial class DictTypeEdit
    {
        private DictionaryType _currentDictionaryType;
        private List<DictionaryItem> _items;

        public DictionaryType CurrentDictionaryType
        {
            get
            {
                return _currentDictionaryType;
            }
            set
            {
                _currentDictionaryType = value;

                DataContext = _currentDictionaryType;

                if (_currentDictionaryType != null)
                    lbDictionaryItem.ItemsSource = _currentDictionaryType.Items;
            }
        }

        public List<DictionaryItem> Items
        {
            get
            {
                if (_items == null)
                    _items = new List<DictionaryItem>();
                return _items;
            }
        }

        public DictTypeEdit(string title)
        {
            InitializeComponent();
            this.Title = title;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(_currentDictionaryType.Name))
            {
                TaskDialogEx.Warning("请输入字典类型中文名称");
                return;
            }
            if (string.IsNullOrEmpty(_currentDictionaryType.Code))
            {
                TaskDialogEx.Warning("请输入字典类型编码");
                return;
            }

            if (_currentDictionaryType.Code.Length > 50)
            {
                TaskDialogEx.Warning("字典类别名称长度不能大于50！");
                return;
            }
            if (_currentDictionaryType.Name.Length > 50)
            {
                TaskDialogEx.Warning("字典类别编码长度不能大于50！");
                return;
            }

            DialogResult = true;
        }

        private void AddedClick(object sender, RoutedEventArgs e)
        {
            var dictionaryItem = new DictionaryItem();
            
           dictionaryItem.Id = System.DateTime.Now.ToString("yyyyMMddHHmmss");//Guid.NewGuid().ToString();
            dictionaryItem.CreateDate = System.DateTime.Now;
            
            dictionaryItem.DictionaryType = _currentDictionaryType;
            _currentDictionaryType.Items.Add(dictionaryItem);
            lbDictionaryItem.SelectedItem = dictionaryItem;
            lbDictionaryItem.ScrollIntoView(dictionaryItem);
        }

        private void btnCronExpression_Click(object sender, RoutedEventArgs e)
        {
            var selectitem = this.lbDictionaryItem.SelectedItem as DictionaryItem;       
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            var dictionaryitem = (DictionaryItem)((Button)sender).Tag;
            _currentDictionaryType.Items.Remove(dictionaryitem);
            if (!string.IsNullOrEmpty(dictionaryitem.Id))
                this.Items.Add(dictionaryitem);
        }
    }

}

